package cn.sytton.taffecloud.service.file.service.impl;

import cn.sytton.taffecloud.common.minio.MinioService;
import cn.sytton.taffecloud.common.servlet.util.ServletUtil;
import cn.sytton.taffecloud.common.model.file.FileEntity;
import cn.sytton.taffecloud.common.model.file.FileGroupEntity;
import cn.sytton.taffecloud.service.file.constants.FileBucket;
import cn.sytton.taffecloud.service.file.mapper.FileGroupMapper;
import cn.sytton.taffecloud.service.file.mapper.FileMapper;
import cn.sytton.taffecloud.service.file.service.IFileService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 公共文件服务
 *
 * @author skyrock
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class FileService implements IFileService {

    @Resource
    private MinioService minioService;
    @Resource
    private FileMapper fileMapper;
    @Resource
    private FileGroupMapper fileGroupMapper;

    @Override
    public FileEntity upload(MultipartFile file, FileBucket bucket) {
        minioService.makeBucket(bucket.getType(), bucket.toString());
        return minioService.upload(file, bucket.toString());
    }

    @Override
    public FileEntity upload(File file, FileBucket bucket) {
        minioService.makeBucket(bucket.getType(), bucket.toString());
        return minioService.upload(file, bucket.toString());
    }

    @Override
    public FileEntity uploadAndSave(MultipartFile file, FileBucket bucket) {
        minioService.makeBucket(bucket.getType(), bucket.toString());
        FileEntity fileEntity = minioService.upload(file, bucket.toString());
        fileMapper.create(fileEntity);
        return fileEntity;
    }

    @Override
    public FileGroupEntity uploadAndSave(List<MultipartFile> files, FileBucket bucket) {
        minioService.makeBucket(bucket.getType(), bucket.toString());
        FileGroupEntity group = new FileGroupEntity();
        group.setFileGroupId(UUID.randomUUID().toString());
        for (MultipartFile file : files) {
            FileEntity fileEntity = minioService.upload(file, bucket.toString());
            fileMapper.create(fileEntity);

            FileGroupEntity fileGroupEntity = new FileGroupEntity();
            fileGroupEntity.setFileGroupId(group.getFileGroupId());
            fileGroupEntity.setFileId(fileEntity.getFileId());
            fileGroupMapper.create(fileGroupEntity);
            group.getFiles().add(fileEntity);
        }
        return group;
    }

    @Override
    public void download(String fileId) {
        FileEntity file = fileMapper.findById(fileId);
        minioService.download(file, ServletUtil.getResponse());
    }

    @Override
    public void downloadGroup(String groupId, String name) {
        List<FileEntity> files = new ArrayList<>();
        List<FileGroupEntity> group = fileGroupMapper.findList(groupId);
        for(FileGroupEntity fg : group) {
            FileEntity file = fileMapper.findById(fg.getFileId());
            files.add(file);
        }
        if (files.size() > 0) {
            if (files.size() == 1) {
                minioService.download(files.get(0), ServletUtil.getResponse());
            } else {
                minioService.downloadZip(files, ServletUtil.getResponse(), name);
            }
        }
    }

    @Override
    public void downloadObject(String objectName, FileBucket bucket) {
        minioService.download(bucket.toString(), objectName, ServletUtil.getResponse());
    }

    @Override
    public String getPublicUrl(String fileId) {
        FileEntity file = fileMapper.findById(fileId);
        return minioService.getPublicUrl(file.getObjectName(), file.getBucketName());
    }

    @Override
    public String getUrl(String fileId) {
        FileEntity file = fileMapper.findById(fileId);
        return minioService.getUrl(file.getObjectName(), file.getBucketName());
    }

    @Override
    public String getUrl(String fileId, long expiryMinute) {
        FileEntity file = fileMapper.findById(fileId);
        return minioService.getUrl(file.getObjectName(), file.getBucketName(), expiryMinute);
    }
}
